package com.ctcemti.mdm.project.sys.service.impl;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.ctcemti.mdm.common.util.FileUploadUtils;
import com.ctcemti.mdm.project.sys.dao.ErrorLogMapper;
import com.ctcemti.mdm.project.sys.entity.ErrorLog;
import com.ctcemti.mdm.project.sys.service.IErrorLogService;
import com.ctcemti.mdm.project.tool.generatecode.util.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 错误日志Service业务层处理
 * 
 * @author bxf
 * @date Tue Nov 12 08:15:44 CST 2019
 */
@Service
public class ErrorLogServiceImpl implements IErrorLogService 
{
    @Autowired
    private ErrorLogMapper errorLogMapper;

    @Value("${ctcemti.errorfilepath}")
    private String errorfilepath;
    /**
     * 查询错误日志
     * bxf
     * @param id 错误日志ID
     * @return 错误日志
     */
    @Override
    public ErrorLog selectErrorLogById(Integer id)
    {
        return errorLogMapper.selectErrorLogById(id);
    }

    /**
     * 查询错误日志列表
     * bxf
     * @param errorLog 错误日志
     * @return 错误日志
     */
    @Override
    public PageInfo<ErrorLog> selectErrorLogList(ErrorLog errorLog)
    {
        PageHelper.startPage(errorLog.getPageIndex(),errorLog.getPageSize(),"tsel.id");
        List<ErrorLog> list=errorLogMapper.selectErrorLogList(errorLog);
        PageInfo<ErrorLog> pageInfo =  new PageInfo<ErrorLog>(list);
        return pageInfo;
    }

    /**
     * 新增错误日志
     * bxf
     * @param errorLog 错误日志
     * @return 结果
     */
    @Override
    public int insertErrorLog(ErrorLog errorLog)
    {
        return errorLogMapper.insertErrorLog(errorLog);
    }

    /**
     * 修改错误日志
     * bxf
     * @param errorLog 错误日志
     * @return 结果
     */
    @Override
    public int updateErrorLog(ErrorLog errorLog)
    {
        return errorLogMapper.updateErrorLog(errorLog);
    }

    /**
     * 删除错误日志对象
     * bxf
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteErrorLogByIds(String ids)
    {
        return errorLogMapper.deleteErrorLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除错误日志信息
     * bxf
     * @param id 错误日志ID
     * @return 结果
     */
    @Override
    public int deleteErrorLogById(Integer id)
    {
        return errorLogMapper.deleteErrorLogById(id);
    }

    @Override
    public void uploadFile(HttpServletResponse response, HttpServletRequest request, String filename) throws SQLException, IOException {
        ErrorLog errorLog = new ErrorLog();
        errorLog.setPageIndex(1);
        errorLog.setPageSize(Integer.MAX_VALUE);
        PageHelper.startPage(errorLog.getPageIndex(),errorLog.getPageSize(),"tsel.id");
        List<ErrorLog> list=errorLogMapper.selectErrorLogList(errorLog);
        ExportParams exportParams = new ExportParams(null,null,filename);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,ErrorLog.class, list);
        FileUploadUtils.downWorkbook(request,response,workbook,filename);

    }

}
